/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 毛超然 on 2017/09/13 15:11.12
 * <p>
 * 溯源Dao层实现
 */
package com.hw.one.peach.core.dao;

import com.hw.one.peach.core.po.TraceDataPO;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Repository(value = "traceAnalysisDao")
public class TraceAnalysisDao implements ITraceAnalysisDao {

    @Resource
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String SITE_ANALYSIS_RESULT = "T_DATA_ANALYSIS_RESULT";

    /**
     * 根据站点编码和监测时间点获取溯源数据
     *
     * @param paramMap
     * @return
     * @throws Exception
     */

    @Override
    public List<TraceDataPO> findTraceabilityDataBySiteCodeAndTime(Map<String, Object> paramMap) throws Exception {
        // 拼凑查询条件
        Criteria criteria = Criteria.where("STANDSITE.SITECODE").is(paramMap.get("siteCode"));
        if (null != paramMap.get("monitorTime")) {
            criteria.and("STANDSITE.MONITORTIME").is(paramMap.get("monitorTime"));
        }
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "STANDSITE.MONITORTIME")).limit(1);

        List<TraceDataPO> resultList = mongoTemplateStandardSiteData.find(query,
            TraceDataPO.class, SITE_ANALYSIS_RESULT);
        return resultList;

    }

    /**
     * 根据站点编码和时间查询一天之内的溯源数据
     *
     * @param paramMap
     * @return
     * @throws Exception
     */

    @Override
    public List<TraceDataPO> findTraceabilityDatasBySiteCodeAndTime(Map<String, Object> paramMap) throws Exception {
        // 拼凑查询条件

        Criteria criteria = Criteria.where("STANDSITE.SITECODE").is(paramMap.get("siteCode"));
        criteria.and("STANDSITE.MONITORTIME").gte(paramMap.get("startTime")).lte(paramMap.get("endTime"));
        // 创建查询对象,增加排序
        Query query = new Query(criteria).with(new Sort(Sort.Direction.DESC, "STANDSITE.MONITORTIME"));

        List<TraceDataPO> resultList = mongoTemplateStandardSiteData.find(query,
            TraceDataPO.class, SITE_ANALYSIS_RESULT);
        return resultList;
    }

}
